home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / envtrim.zip / ENVTRIM.C < prev    next >
C/C++ Source or Header  |  1992-08-31  |  4KB  |  112 lines

  1. /* envtrim.c
  2.    Program to read and re-write environment variables for MS-DOS
  3.    Written by H. D. Todd, Computing Center, Wesleyan University, 12/26/90,
  4.    using prototypes written by Douglas Bigelow, same address.
  5.    Contact: hdtodd@mockingbird.wesleyan.edu
  6.    
  7.    envtrim trims from the left or right ("l" or "r") to a specified
  8.    count of characters ("nn") the value associated with environment
  9.    variable  "var".   One or more environment variables can be truncated
  10.    in a single  invocation of envtrim.  The environment variables in the
  11.    PARENT process are  modified.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <dos.h>
  18. #define MAXSTRS 100        /* increase this if you have more env strings */
  19.  
  20. void main(int argc, char *argv[])
  21.  
  22. {
  23.   int i, num, envsize, len, nstrs=0, nextra;
  24.   unsigned far *x1, far *x2;
  25.   char dowhat, varstg[100], *varval, far *envp, far *envptr;
  26.   char *strptr[MAXSTRS];
  27.   unsigned far *p_psp, far *p_env;
  28.   struct {
  29.          char link;
  30.          unsigned int owner_psp;
  31.          unsigned int blk_len;
  32.          } far *envmcb;
  33.  
  34.   if (argc < 2) {
  35.     fprintf(stderr, "Usage: envtrim -{lr}nn var [-{lr}nn var] ...\n");
  36.     exit(1); }
  37.  
  38. /* Construct the PSP pointers -- first find the root PSP*/
  39.   x1 = MK_FP(_psp,0x16);
  40.   x2 = MK_FP(*x1,0x10);
  41. /*  printf("x1=%Fp   x2=%Fp\n",x1,x2); */
  42.   while (x1 != x2)
  43.      {x1 = x2;
  44.       x2 = MK_FP(*x1,0x10);
  45. /*      printf("x1=%Fp   x2=%Fp\n",x1,x2); */
  46.      }
  47. /* We now have the pointer to the root COMMAND.COM PSP*/
  48.  
  49.   x1 = MK_FP(*x1,0x2c);      /* pointer to root environment ptr */
  50.   envptr = MK_FP(*x1,0x00);     /* pointer to root environment */
  51.   envmcb = MK_FP((*x1)-1,0x00); /* pointer to root env MCB */
  52.   envsize = (*envmcb).blk_len<<4;
  53.  
  54. /* copy strings from root environment (FAR) to local strings */
  55.    envp = envptr;
  56.    while (*envp) {      /* env strings terminated by second null */
  57.          int len;
  58.          char far *fp;
  59.          char *lp;
  60.          for (len=0, fp=envp; *fp; fp++) len++;
  61.          lp = strptr[nstrs++] = malloc(len+1);
  62.          for ( ; (*lp++=*envp++); );
  63.          }
  64.  
  65.     while (--argc > 0 ) {
  66.       if ( ( (dowhat=(*++argv)[0])) != '-') {
  67.          fprintf(stderr, "envtrim: begin switches with '-', as in '-%c'\n", dowhat);
  68.          exit(1);
  69.       };
  70.           dowhat = tolower(*++argv[0]);
  71.       if (dowhat!='l' && dowhat!='r') {
  72.          fprintf(stderr, "envtrim: invalid switch -%c\n", dowhat);
  73.          exit(1);
  74.       };
  75.       if ( ((i=sscanf(++argv[0],"%d",&num)) == EOF) || (i==0) ) {
  76.              fprintf(stderr, "envtrim: can't scan number for arg -%c\n",dowhat);
  77.              exit(1);
  78.           };
  79.  
  80. /*  now look in the table of env strings to see if we match the one
  81.     we're supposed to edit */
  82.  
  83.           ++argv; --argc;
  84.           strcpy(varstg,argv[0]);
  85.           strcat(varstg,"=");
  86.           for (i=0; i<nstrs; i++)
  87.               if (strnicmp(strptr[i],varstg,strlen(varstg))==0) break;
  88.           if (i>=nstrs)
  89.                fprintf(stderr, "envtrim: Can't find environment var '%s'\n", varstg);
  90.             else {
  91.                  varval = strdup(strptr[i]+strlen(varstg));
  92.                  if (strlen(varval)>num) {
  93.                     if (dowhat == 'l') varval[num] = 0;
  94.                     if (dowhat == 'r') varval += strlen(varval)-num;
  95.                  };
  96.                  strcpy(strptr[i]+strlen(varstg), varval);
  97.             }
  98.     }
  99.  
  100.     for (i=0, len=0; i<nstrs; i++) len += strlen(strptr[i]) + 1;
  101.     if (len+1>envsize)  {
  102.         fprintf(stderr,"envtrim: Env block too small to store updated environment strings\n");
  103.         exit(1);
  104.         }
  105.       else {
  106.         for (i=0; i<nstrs; i++) {
  107.             for ( ; (*envptr++ = *strptr[i]++) ; ) ;
  108.             *envptr = '\0';
  109.         }
  110.       }
  111. }
  112.